import { ScriptConfType } from '@common'
import { BaseModelTreeNodeType } from '@renderer/types'

export type ScriptConfTreeNodeType = ScriptConfType &
  BaseModelTreeNodeType & {
    children?: ScriptConfTreeNodeType[]
  }

export type ScriptConfTreeType = ScriptConfTreeNodeType[]

export const useScriptConfStore = defineStore('scriptConfStore', () => {
  const { getMap, getList, getTree, getDirTree, getTreeMapAll, getTreeMap, load } =
    genAndLoadConfStatus(api.findScriptConfs) as {
      getMap: () => Record<string, ScriptConfType>
      getList: () => ScriptConfTreeType
      getTree: () => ScriptConfTreeType
      getDirTree: () => ScriptConfTreeType
      getTreeMapAll: () => Record<string, ScriptConfTreeNodeType>
      getTreeMap: () => Record<string, ScriptConfTreeNodeType>
      load: () => Promise<void>
    }
  const scriptConfSelected = ref<ScriptConfType>(genScriptConf())
  const getScriptConfSelected = () => scriptConfSelected.value as ScriptConfType
  const setScriptConfSelected = (id: string) => (scriptConfSelected.value = getMap()[id])

  return {
    getMap,
    getList,
    getTree,
    getDirTree,
    getTreeMapAll,
    getTreeMap,
    load,
    getScriptConfSelected,
    setScriptConfSelected
  }
})
